package com.example.weatherforecast;

/**
 * @Author：coderChen
 * @Package：com.example.weatherforecast
 * @Project：WeatherForecast
 * @Date：2024/1/1 1:35
 * @Filename：AppInfoDialog
 * Explain:应用程序信息
 */
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.example.weatherforecast.R;

public class AppInfoDialog {

    public static void showAppInfoDialog(Context context) {
        // 获取应用程序的名称和版本信息
        String appName = context.getString(R.string.app_name);
        String appVersion = getAppVersion(context);

        // 获取开发者信息
        String developerInfo = context.getString(R.string.developer_info);

        // 创建并显示AlertDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View dialogView = inflater.inflate(R.layout.dialog_app_info, null);
        builder.setView(dialogView);

        TextView appNameTextView = dialogView.findViewById(R.id.app_name);
        TextView appVersionTextView = dialogView.findViewById(R.id.app_version);
        TextView developerInfoTextView = dialogView.findViewById(R.id.developer_info);

        appNameTextView.setText(appName);
        appVersionTextView.setText(appVersion);
        developerInfoTextView.setText(developerInfo);

        builder.setPositiveButton(android.R.string.ok, null);
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private static String getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            String versionName = packageInfo.versionName;
            return "Version: " + versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }

}

